<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入竞赛名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="组别" prop="groupType">
        <el-select v-model="queryParams.groupType" clearable placeholder="请选择" style="width: 140px;">
          <el-option label="本科组" :value="1" />
          <el-option label="专科组" :value="2" />
          <el-option label="研究生组" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="competitionStatus">
        <el-select v-model="queryParams.competitionStatus" clearable placeholder="请选择" style="width: 140px;">
          <el-option label="可报名" :value="1" />
          <el-option label="截止报名" :value="2" />
          <el-option label="竞赛中" :value="3" />
          <el-option label="已完赛" :value="4" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['race:competition:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['race:competition:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['race:competition:remove']">删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" prop="id" align="center" width="80" />
      <el-table-column label="名称" prop="name" align="center" min-width="150" />
      <el-table-column label="描述" prop="description" align="center" min-width="200" show-overflow-tooltip />
      <el-table-column label="组别" prop="groupType" align="center" width="120">
        <template #default="scope">
          <el-tag v-if="scope.row.groupType===1">本科组</el-tag>
          <el-tag type="warning" v-else-if="scope.row.groupType===2">专科组</el-tag>
          <el-tag type="success" v-else>研究生组</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="竞赛状态" prop="competitionStatus" align="center" width="120">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.competitionStatus)" size="small">
            {{ getStatusText(scope.row.competitionStatus) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="报名时间" align="center" width="200">
        <template #default="scope">
          <div>{{ formatDate(scope.row.registrationStartTime) }}</div>
          <div>至 {{ formatDate(scope.row.registrationEndTime) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="竞赛时间" align="center" width="200">
        <template #default="scope">
          <div>{{ formatDate(scope.row.startTime) }}</div>
          <div>至 {{ formatDate(scope.row.endTime) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['race:competition:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['race:competition:remove']">删除</el-button>
          <el-button link type="primary" icon="View" @click="handleDetail(scope.row)" size="small">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="竞赛名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入竞赛名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="竞赛组别" prop="groupType">
              <el-select v-model="form.groupType" placeholder="请选择" style="width: 100%">
                <el-option label="本科组" :value="1" />
                <el-option label="专科组" :value="2" />
                <el-option label="研究生组" :value="3" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-form-item label="竞赛描述" prop="description">
          <el-input v-model="form.description" type="textarea" :rows="3" placeholder="请输入竞赛描述" />
        </el-form-item>
        
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="竞赛状态" prop="competitionStatus">
              <el-select v-model="form.competitionStatus" placeholder="请选择" style="width: 100%">
                <el-option label="可报名" :value="1" />
                <el-option label="截止报名" :value="2" />
                <el-option label="竞赛中" :value="3" />
                <el-option label="已完赛" :value="4" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="报名开始时间" prop="registrationStartTime">
              <el-date-picker v-model="form.registrationStartTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择报名开始时间" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报名结束时间" prop="registrationEndTime">
              <el-date-picker v-model="form.registrationEndTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择报名结束时间" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="竞赛开始时间" prop="startTime">
              <el-date-picker v-model="form.startTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择竞赛开始时间" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="竞赛结束时间" prop="endTime">
              <el-date-picker v-model="form.endTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="选择竞赛结束时间" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        
        
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="3" placeholder="备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup>
import { ref, reactive, toRefs, getCurrentInstance } from 'vue'
import { useRouter } from 'vue-router'
import { listCompetition, getCompetition, addCompetition, updateCompetition, delCompetition } from '@/api/race/competition'

const { proxy } = getCurrentInstance()
const router = useRouter()

const list = ref([])
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
const open = ref(false)
const title = ref('')
const ids = ref([])
const single = ref(true)
const multiple = ref(true)

const data = reactive({
  form: {
    id: undefined,
    name: undefined,
    description: undefined,
    groupType: undefined,
    competitionStatus: 1,
    registrationStartTime: undefined,
    registrationEndTime: undefined,
    startTime: undefined,
    endTime: undefined,
    remark: undefined
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    groupType: undefined,
    competitionStatus: undefined
  },
  rules: {
    name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
    groupType: [{ required: true, message: '请选择组别', trigger: 'change' }],
    competitionStatus: [{ required: true, message: '请选择竞赛状态', trigger: 'change' }],
    registrationStartTime: [{ required: true, message: '请选择报名开始时间', trigger: 'change' }],
    registrationEndTime: [{ required: true, message: '请选择报名结束时间', trigger: 'change' }],
    startTime: [{ required: true, message: '请选择开始时间', trigger: 'change' }],
    endTime: [{ required: true, message: '请选择结束时间', trigger: 'change' }]
  }
})

const { form, queryParams, rules } = toRefs(data)

function getList() {
  loading.value = true
  listCompetition(queryParams.value).then(res => {
    list.value = res.rows
    total.value = res.total
    loading.value = false
  })
}

function cancel() {
  open.value = false
  reset()
}

function reset() {
  form.value = {
    id: undefined,
    name: undefined,
    description: undefined,
    groupType: undefined,
    competitionStatus: 1,
    registrationStartTime: undefined,
    registrationEndTime: undefined,
    startTime: undefined,
    endTime: undefined,
    remark: undefined
  }
  proxy.resetForm('formRef')
}

function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

function handleAdd() {
  reset()
  open.value = true
  title.value = '新增竞赛'
}

function handleUpdate(row) {
  reset()
  const id = row.id || ids.value[0]
  getCompetition(id).then(res => {
    form.value = res.data
    open.value = true
    title.value = '修改竞赛'
  })
}

function submitForm() {
  proxy.$refs['formRef'].validate(valid => {
    if (!valid) return
    const req = form.value.id ? updateCompetition(form.value) : addCompetition(form.value)
    req.then(() => {
      proxy.$modal.msgSuccess(form.value.id ? '修改成功' : '新增成功')
      open.value = false
      getList()
    })
  })
}

function handleDelete(row) {
  const delIds = row.id ? [row.id] : ids.value
  proxy.$modal.confirm('是否确认删除编号为"' + delIds.join(',') + '"的数据项？').then(() => {
    return delCompetition(delIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess('删除成功')
  }).catch(() => {})
}

/** 详情按钮操作 */
function handleDetail(row) {
  router.push('/race/competition/detail/' + row.id)
}


/** 获取状态类型 */
function getStatusType(status) {
  const statusMap = {
    1: 'success',  // 可报名
    2: 'warning',  // 截止报名
    3: 'primary',  // 竞赛中
    4: 'info'      // 已完赛
  }
  return statusMap[status] || 'info'
}

/** 获取状态文本 */
function getStatusText(status) {
  const statusMap = {
    1: '可报名',
    2: '截止报名',
    3: '竞赛中',
    4: '已完赛'
  }
  return statusMap[status] || '未知'
}

/** 格式化日期 */
function formatDate(date) {
  if (!date) return '-'
  return new Date(date).toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

getList()
</script>


